<?php

class AOrdenMCheckList {

    public function __construct(){
	}
	public function creaOrdenesTrabajo(EFacade & $facade)
	{
		
		$docOMCh = & $facade->getDocumentRoot();
		$workflow = & $facade->getWorkflow();
		$listaBuses = & $docOMCh->instanciaBuses;
		
		
		$ezpdo_=getManager();
		$cant = 0;
		echo '<br>cantidad original ',$cantidadPersonal = & $docOMCh->asociadoA->cantidadEmpleado;
		$cantidadPersonal = $cantidadPersonal - 1;
		
		if($cantidadPersonal > -1){
			if($cantidadPersonal == 0){
				$empleado = $docOMCh->asociadoA->personalAsignado[$cantidadPersonal]->esRealizadaPor;
			}
			else{
				$empleado = $docOMCh->asociadoA->personalAsignado[$cantidadPersonal-1]->esRealizadaPor;
			}
			$cantidadPersonal = $cantidadPersonal-1;
			$docOMCh->asociadoA->cantidadEmpleado = $cantidadPersonal;
			echo '<hr> cantiad ',$cantidadPersonal,' -- ',$empleado,' -- ';
		}
		else{
			$cantidadPersonal = count($docOMCh->asociadoA->personalAsignado);
			$docOMCh->asociadoA->cantidadEmpleado = $cantidadPersonal;
			//$user->setAttribute('cantidad', $cantidadPersonal, 'tmp');
			$empleado = $docOMCh->asociadoA->personalAsignado[$cantidadPersonal-1]->esRealizadaPor;
			echo '<hr> cantiad else ',$cantidadPersonal,' -- ',$empleado,' -- ';
		}
		
		if($listaBuses = & $docOMCh->instanciaBuses){
		//foreach($listaBuses as $bus){
	        
			echo '<br>si crea ',$ordenTrabajo=$facade->session->create('DOrdenTrabajo');
			
			echo '<hr>crea - ',$ordenTrabajo->ordenMantenimiento=$docOMCh;
			echo '<br><br>si empleado ',$ordenTrabajo->empleado = $empleado;
			$ordenTrabajo->listaEquipos = $docOMCh->instanciaBuses;
//				foreach($ordenTrabajo->personalAsignado as $r)
//				echo '<br>si  ',$r;
//			$cant++;
			
			$role=NULL;
			$newEfacade =& EFacade :: initWorkflow($facade->session, $ordenTrabajo, & $role);
			$transSet = & EFacade :: getCurrentTransSetAuto($newEfacade->getWorkflow(),3);//tipo process
			$trans = $transSet->getTransByName('t1');
			$usuario=NULL;
			$newEfacade->fireTrans($trans, $usuario, "Crear",TRUE);
			echo '<hr>- trbajo  ';
			$ordenTrabajo->epSetDirty(TRUE);// marcar el documento para que sea salvado los cambios
			$newEfacade->saveWorkflow();
			$ordenTrabajo->commit();
			
			$ordenTrabajo->numero = $ordenTrabajo->getDocumentNumber();
			$ordenTrabajo->commit();
			echo '<hr>- la orden de trbajo  ',$ordenTrabajo;
			unset($newEfacade);
//				$facade->session->evictAll('MEdge');
//				$facade->session->evictAll('MTrans');
//				$facade->session->evictAll('MPlace');
		
		}		
		$docOMCh->epSetDirty(TRUE);
		
		$workflowOMC = & $facade->getWorkflow();
		$workflowOMC->epSetDirty(TRUE);

		$transSetOMC = & EFacade :: getCurrentTransSetAuto($workflowOMC, /* MTrans :: $TYPE_AUTO_AGENT */ 2);
		$listaWT= &$workflowOMC->getWorkTransList();

		$transOMC = $transSetOMC->getTransByName('t2');
		$facade->fireTrans($transOMC,$usuario,'Creacion de Ordenes', TRUE);	
		$facade->saveWorkflow();
	}
	
	public function continuar(EFacade & $facade)
	{
		
		$docOMCh = & $facade->getDocumentRoot();
		$workflow = & $facade->getWorkflow();
		$ezpdo_=getManager();
	
		echo '<hr>- docOMCh  ',$docOMCh;
		echo '<hr>- docOt  ',$docOT = $docOMCh->ordenesTrabajo[0];
		echo '<br><br> coun--> ',count($docOMCh->ordenesTrabajo);
		
		$role=NULL;
		$workflowOT = $docOT->getWorkflowRef();
//		$facade->session->refresh($workflowOT);
		
		$newEfacade =& EFacade :: initWorkflow($facade->session, $docOMCh, & $role);
//		echo '<br> ** ',$transSet = & EFacade :: getCurrentTransSetAuto($newEfacade->getWorkflow(),2);//tipo agent
		//$trans = $transSet->getTransByName('t1');
		
		//$workflowOT=$docOT->getWorkflowRef();
		
		
		//$facade->session->evictAll('MWorkPlace');
		$placeSetOT = & EFacade :: getCurrentPlaceSet($workflowOT);
		echo '<hr>  place ',$placeSetOT;

		$usuario=NULL;
		//////die;
		if($placeSetOT->name == 'ps11'){
			
			echo '<br>si crea SOMCh ',$solicitudOMCh = $facade->session->create('DSolicitudOrdenMCheckList');
			$solicitudOMCh->ordenMCheckListPrincipal = $docOMCh;

			$role=NULL;
			$newEfacade1 =& EFacade :: initWorkflow($facade->session, $solicitudOMCh, & $role);
			$transSet1 = & EFacade :: getCurrentTransSetAuto($newEfacade1->getWorkflow(),3);//tipo process
			$trans1 = $transSet1->getTransByName('t1');
			$usuario=NULL;
			$newEfacade1->fireTrans($trans1, $usuario, "Crear",TRUE);
			echo '<hr>- SOMCh-  ';
			$solicitudOMCh->epSetDirty(TRUE);// marcar el documento para que sea salvado los cambios
			$newEfacade1->saveWorkflow();
			$solicitudOMCh->commit();
			
			$solicitudOMCh->numero = $solicitudOMCh->getDocumentNumber();
			$solicitudOMCh->commit();
			echo '<hr> la SOMCh  ',$solicitudOMCh;
			unset($newEfacade1);
			
			$workflowOMC = & $facade->getWorkflow();
			$workflowOMC->epSetDirty(TRUE);
	
			echo '<br><br>-transSetOMC  ',$transSetOMC = & EFacade :: getCurrentTransSetAuto($workflowOMC, /* MTrans :: $TYPE_AUTO_AGENT */ 2);
			$listaWT= &$workflowOMC->getWorkTransList();
	
			echo '<br><br>- transOMC  ',$transOMC = $transSetOMC->getTransByName('t4');
			$facade->fireTrans($transOMC,$usuario,'Creacion de SolicitudOMCh', TRUE);	
		
		
			$newEfacade->saveWorkflow();
			echo '<br> <br> CREAR SOLICITUDOMCH y CULMINA OMCh';
			//die;
		}
		else{
			if($placeSetOT->name == 'ps9'){
				$role = NULL;
				echo '<hr> esto es docomch ',$docOMCh;
				$workflowOMC = & $facade->getWorkflow();
				echo '<hr> docomch ',$workflowOMCh = $docOMCh->getWorkflowRef();
				
				$solicitudBus = $docOMCh->asociadoA;
				
				echo '<br><br>-ps9 transSetOMC  ',$transSetOMC = & EFacade :: getCurrentTransSetAuto($workflowOMCh, /* MTrans :: $TYPE_AUTO_AGENT */ 2);
				//$listaWT= &$workflowOMC->getWorkTransList();
		
				echo '<br><br>-ps9  transOMC  ',$transOMC = $transSetOMC->getTransByName('t5');
				$facade->fireTrans($transOMC,$usuario,'Creacion de SolicitudOMCh', TRUE);
			
				echo '<br> <br> ps9 CONTINUA LA OMCH';
				$workflowOMCh->epSetDirty(TRUE);
				$newEfacade->saveWorkflow();
				
//				echo '<br><br> 1-->> ',$workflowSB = & $solicitudBus->getWorkflowRef();
//				echo '<br><br> 2-- ',$newEfacade1 = & EFacade :: initWorkflowWithoutRole($facade->session, $solicitudBus, /* MTrans :: $TYPE_AUTO_PROCESS */ 3);
//				echo '<br><br> 3<<-->> ',$transSetSB = & EFacade :: getCurrentTransSetAuto($workflowSB, /* MTrans :: $TYPE_AUTO_PROCESS */ 3);
//				echo '<br><br> 4<<-->> ',$transSB = $transSetSB->getTransByName('t4');
//				$newEfacade1->fireTrans($transSB,$role,'Info ChekcList', TRUE);
//				
//				$solicitudBus->epSetDirty(TRUE);
				//$workflowSB->saveWorkflow();
				//$facade->session->refresh($workflowOMC);	
		

				//die;
			}
		}
	}
	
	public function culminar(EFacade & $facade)
	{
		$docOMCh = & $facade->getDocumentRoot();
		$workflow = & $facade->getWorkflow();
		$ezpdo_=getManager();
		echo '<br><br> sb ',$solicitudBus = $docOMCh->asociadoA;
		
		
		$role=NULL;
		$newEfacade2 =& EFacade :: initWorkflowWithoutRole($facade->session, $docOMCh,/* MTrans :: $TYPE_AUTO_PROCESS */ 2);
		$newEfacade2->saveWorkflow();
		$docOMCh->commit();
		
		echo '<br><br> -->> ',$workflowSB = & $solicitudBus->getWorkflowRef();
		//$facade->session->refresh($workflowSB);
		echo '<br><br> -- ',$newEfacade = & EFacade :: initWorkflowWithoutRole($facade->session, $solicitudBus, /* MTrans :: $TYPE_AUTO_PROCESS */ 3);
		echo '<br><br> <<-->> ',$transSetSB = & EFacade :: getCurrentTransSetAuto($newEfacade->getWorkflow(), /* MTrans :: $TYPE_AUTO_PROCESS */ 3);
		echo '<br><br> e--e ',$transSB = $transSetSB->getTransByName('t4');
		echo '<br><br> s--s ',$transSetSB->getTransByName('t5');
		$newEfacade->fireTrans($transSB,$role,'En espera de ChekList', TRUE);
		echo '<br><br> 11<<-->>11 ';
		$solicitudBus->epSetDirty(TRUE);
		$newEfacade->saveWorkflow();
		echo '<br><br> 22<<-->>22 ';
		
		$newEfacade2 =& EFacade :: initWorkflowWithoutRole($facade->session, $docOMCh,/* MTrans :: $TYPE_AUTO_PROCESS */ 2);
		echo '<br> ** ',$transSetOMC = & EFacade :: getCurrentTransSetAuto($newEfacade2->getWorkflow(),2);//tipo agent
		//$listaWT = &$workflow->getWorkTransList();
		
		echo '<br><br>-ps6  transOMC  ',$transOMC = $transSetOMC->getTransByName('t6');
		$newEfacade2->fireTrans($transOMC,$role,'Agente Culmina Solicitud de Buses', TRUE);
		
		$docOMCh->epSetDirty(TRUE);
		$newEfacade2->saveWorkflow();
				
		echo '<br> <br> CULMINO LA OMCH';
				
	}
}
?>